﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Acms.Bll;

public partial class Posts_Thumbnail : BasePostPage
{
    private Role.Roles[] EditRoles = new Role.Roles[] { Role.Roles.Developer, Role.Roles.ContentEditor, Role.Roles.ContentPublisher };

    public override Role.Roles[] AllowedRoles
    {
        get { return new Role.Roles[] { Role.Roles.Developer, Role.Roles.ContentViewer, Role.Roles.ContentEditor, Role.Roles.ContentPublisher }; }
    }

    protected void Page_Load(object sender, EventArgs e)
    {
        dsSummary.SelectData += new EventHandler<Acms.DataControls.SelectEventArgs<Post>>(dsSummary_SelectData);
        if (!IsPostBack)
        {
            lnkEdit.Visible = User.HasRole(EditRoles);
            pnlUploadThumb.Visible = false;
            lnkSave.Visible = false;
            lnkCancel.Visible = false;
            DataBind();
        }
    }

    void dsSummary_SelectData(object sender, Acms.DataControls.SelectEventArgs<Post> e)
    {
        e.SetData(base.CurrentPost);
    }

    public override void DataBind()
    {
        if (CurrentPost == null || CurrentPost.ThumbnailId == null)
        {
            pnlNoThumb.Visible = true;
            colThums.Visible = false;
        }
        else
        {
            pnlNoThumb.Visible = false;
            colThums.Visible = true;
            img150150.FileId = CurrentPost.ThumbnailId;
            img100100.FileId = CurrentPost.ThumbnailId;
            img5050.FileId = CurrentPost.ThumbnailId;
            img2525.FileId = CurrentPost.ThumbnailId;
        }
        dvSummary.DataBind();
    }

    protected void lnkSave_Click(object sender, EventArgs e)
    {
        if (upload.UploadedFiles.Count == 1)
        {
            Acms.Bll.File file = Acms.Bll.File.CreateInstance(Acms.Bll.File.FileTypes.Image, upload.UploadedFiles[0].GetNameWithoutExtension(), upload.UploadedFiles[0].ContentType, upload.UploadedFiles[0].ContentLength, upload.UploadedFiles[0].InputStream);

            file.BeginTransaction();
            file.Update();

            CurrentPost.UseTransaction(file.TransactionKey);
            CurrentPost.RequiresPublishing = true;
            CurrentPost.ThumbnailId = file.Id;
            CurrentPost.Update();
                
            CurrentPost.CommitTransaction();
            DataBind();
        }
        lnkEdit.Visible = User.HasRole(EditRoles);
        lnkSave.Visible = false;
        lnkCancel.Visible = false;
        pnlUploadThumb.Visible = false;
    }
    
    protected void lnkEdit_Click(object sender, EventArgs e)
    {
        lnkEdit.Visible = false;
        lnkSave.Visible = true;
        lnkCancel.Visible = true;
        pnlUploadThumb.Visible = true;

    }

    protected void lnkCancel_Click(object sender, EventArgs e)
    {
        lnkEdit.Visible = User.HasRole(EditRoles);
        lnkSave.Visible = false;
        lnkCancel.Visible = false;
        pnlUploadThumb.Visible = false;
    }

    public override void GetBreadCrumbs(BaseMasterPage.BreadCrumbCollection breadcrumbs)
    {
        base.GetBreadCrumbs(breadcrumbs);
        breadcrumbs.Add("Thumbnails");
    }

}
